<!DOCTYPE HTML>
<html>
<head>
    <title>Todo</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="/scripts/jquery.min.js"></script>
    <link rel="stylesheet" href="/scripts/miniui/themes/default/miniui.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/icons.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/bootstrap/skin.css">
    <link rel="stylesheet" href="/editor/common.css">
    <script src="/scripts/miniui/miniui.js"></script>
</head>
<body style="padding: 0;width: 100%;height: 100%;margin: 0">
<div class="mini-toolbar" style="text-align: center; line-height: 30px; border: 0px;" borderstyle="border:0;">
    <span>部落名称：</span><input class="mini-textbox" type="text" id="name"/>

    <span>部落id：</span><input class="mini-textbox" type="text" id="objectId"/>

    <input style="margin-right: 30px" type="button" value="查找" onclick="search()"/>
</div>
<div id="datagrid1" class="mini-datagrid" style="width:100%;height: 80%"
     url="/tribeRoute/regTribeFind" idField="id" allowResize="true"
     sizeList="[10,30,50,100]" pageSize="10"
>
    <div property="columns">
        <div type="checkcolumn" width="20"></div>
        <div field="objectId" width="80" align="center" headerAlign="center">部落ID</div>
        <div field="name" width="50" align="center" headerAlign="center">部落名称</div>
        <div field="tribeAvator" width="30" align="center" headerAlign="center">部落头像</div>
        <div field="createdAt" width="100" renderer="onBirthdayRenderer" align="center" headerAlign="center"
             allowSort="true">创建日期
        </div>
    </div>
</div>
<div class="mini-toolbar" style="text-align: center; padding-top: 8px; padding-bottom: 8px; border: 0px;"
     borderstyle="border:0;">
    <a class="mini-button" href="javascript:void(0)" style="width: 60px;"><span class="mini-button-text "
                                                                                onclick="onOk()">确定</span></a>
    <span style="display:inline-block;width:25px;"></span>
    <a class="mini-button" href="javascript:void(0)" style="width: 60px;"><span class="mini-button-text "
                                                                                onclick="onCancel()">取消</span></a>
</div>

</body>
<script type="text/javascript">
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.load();
    function search() {
        var name = mini.get("name").getValue();
        var objectId = mini.get("objectId").getValue();
        grid.load({
            name: name,
            objectId: objectId,
        });
    }
    $("#name,#objectId,#tel").bind("keydown", function (e) {
        if (e.keyCode == 13) {
            search();
        }
    });

    function onBirthdayRenderer(e) {
        var value = e.value;
        if (value) return mini.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
        return "";
    }
    function CloseWindow(action) {
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();
    }

    function saveData() {
        let row = grid.getSelected();
        if (row) {
            let objectId = row.objectId;
            $.ajax({
                url: '/tribeRoute/findRegTribe',
                type: 'post',
                data: {
                    objectId: objectId
                },
                success: function (result) {
                    if (result) {
                        alert("此部落已经在推荐中");
                        CloseWindow("ok");
                        return;
                    }
                    else {
                        insertRegTribeDefault(objectId);
                    }
                },
                error: function (error) {
                    console.log(error)
                }
            })
        }
    }

    function insertRegTribeDefault(objectId) {
        $.ajax({
            url: '/tribeRoute/insertRegTribeDefault',
            type: 'post',
            data: {
                objectId
            },
            success: function (result) {
                if (result) {
                    alert("保存成功！");
                    CloseWindow("ok")
                }
            },
            error: function (error) {
                console.log(error)
            }
        })
    }
    function onOk() {
        saveData()
    }
    function onCancel() {
        CloseWindow("cancel");
    }
</script>
</html>